home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6318 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: changing strings via pointers
  5. Date: 23 Feb 1996 21:47:38 GMT
  6. Organization: systems hk
  7. Message-ID: <4glclq$ovu@maureen.teleport.com>
  8. References: <1996Feb22.125436.25503@leeds.ac.uk>
  9. NNTP-Posting-Host: ip-pdx04-40.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. csyamc@scs.leeds.ac.uk (A M Casey) wrote:
  16. [snip]
  17. >The thing is, I want to get rid of the "\n" and replace it with "\0", but
  18. >obviously I can only do this using the pointer. I'm using the following code:
  19. >
  20. >char *Contents < pointer to string
  21. >int count = 0;
  22. >
  23. >while (*(Contents + count)!= NULL)  /* the end of string is not reached */
  24. >{if (*(Contents + count) == atoi("\n"))
  25. >/* change the \n to a \0 and set the next char along to null */
  26. >{
  27. >*(Contents + count) = atoi("\0");
  28. >
  29. >*(Contents + count + 1) = NULL;
  30. >} 
  31. >else
  32. >/* read in the next char */
  33. >{
  34. >count++;
  35. >
  36. >I think the problem maybe my way of using the pointer - I can print out each
  37. >char as I get to it, but it doesnt seem to change the array at all.
  38. >Should I be using atoi?, it doesnt seem to work without it.
  39. >
  40. [snip]
  41. Andy,
  42. Assuming that you have allocated memory for 'Contents', I would
  43. suggest the following alternative to strip new-lines (no loop
  44. required):
  45.  
  46.   ...
  47.   char  *pnl;
  48.   ...
  49.   if( (pnl=strchr(Contents,'\n')) )
  50.     *pnl = '\000';
  51.   ...
  52.  
  53. Or change your use of '...== atoi('\n') to that of simply testing each
  54. character with '\n':
  55.   ...
  56.   {if (*(Contents + count) == '\n') )
  57.   ...
  58.  
  59. Yours, Geoff Houck
  60.  
  61.